package com.shenrongrong.controller;

import com.github.pagehelper.PageInfo;
import com.shenrongrong.common.Result;
import com.shenrongrong.model.Goods;
import com.shenrongrong.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController

public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @GetMapping("/goods")
    public Result selectAllGoods(@RequestParam(defaultValue = "1") Integer pageNum,
                                 @RequestParam(defaultValue = "6") Integer pageSize,
                                 Goods goods) {
        System.out.println("selectAllGoods");
        PageInfo<Goods> pageInfo = goodsService.selectAllGoods(pageNum, pageSize, goods);
//        return goodsService.selectAllGoods(goods);
//        return Result.success("查询成功");
        return Result.success(pageInfo);
    }
    @GetMapping("/tipGoods")
    public Result selectTipGoods(Integer userid) {
        System.out.println("selectTipGoods");
        return Result.success(goodsService.selectTipGoods(userid));
    }

    @PostMapping("/insertGoods")
    public Result insertGoods(@RequestBody Goods goods) {
        System.out.println("insertGoods");
        boolean isgoodExists = goodsService.isgoodUnique(goods.getName(),goods.getLocation(),goods.getGoodid());
        if (isgoodExists) {
            goodsService.insertGoods(goods);
            return Result.success();
        }
        return Result.error("该位置存在"+goods.getName());
    }

    @DeleteMapping("/deleteGoods/{goodid}")
    public Result deleteGoods(@PathVariable Integer goodid) {
        System.out.println("deleteGoods");
        int deleteList = goodsService.deleteGoods(goodid);
        return Result.success(deleteList);
    }

    @GetMapping("/classification/{userid}")
    public Result selectByclassification(@PathVariable Integer userid) {
        System.out.println("selectByclassification");
        List<Map<String,String>> goodsList = goodsService.selectByClassification(userid);
        return Result.success(goodsList);
    }

    @PutMapping("/updateGoods/{goodid}")
    public Result updateGoodsNumber(@PathVariable Integer goodid,@RequestParam Integer number) {
        System.out.println("updateGoods");
        return Result.success(goodsService.updateGoodsNumber(goodid,number));
    }

    @PutMapping("/updateGoods")
    public Result updateGoods(@RequestBody Goods goods) {
        System.out.println("updateGoods");
        boolean isgoodExists = goodsService.isgoodUnique(goods.getName(),goods.getLocation(),goods.getGoodid());
        if (isgoodExists) {
            return Result.success(goodsService.updateGoods(goods));
        }
        return Result.error("该位置存在"+goods.getName());
    }

}
